package com.java.yz.day15;

public class ProducerDemo1 extends Thread{
    private Student student;
    int i=0;
    public ProducerDemo1() {
    }

    public ProducerDemo1(Student student) {
        this.student = student;
    }

    @Override
    public void run() {
        //Student s1 = new Student();
        while (true){
            synchronized (student){   //这把锁就是student对象
                //如果对象没有创建，就创建对象
                if (student.flag){  //如果为false就是说明对象没创建好，要等待
                    //等待来消费
                    //这里通过锁对象来调用wait方法等待
                    try {   //线程变成阻塞状态
                        student.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }


                //true就创建对象
                    if (i%2==0){
                        student.name="yz";
                        student.age=20;
                    }else {
                        student.name="hy";
                        student.age=21;
                    }
                    //创建完对象后就赋值为true
                student.flag=true;
                //还要通知消费者来消费
                student.notify();
                i++;
            }
        }
    }
}
